using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using Microsoft.Xna.Framework.Content;

namespace Dracham.TileManagement
{
    #region Exceptions

    public class ZoneNotFoundException : Exception
    {
        public ZoneNotFoundException(String zoneId):base("Zone '" + zoneId + "' not found!")
        {

        }
    }

    #endregion

    public class ZoneFactory
    {
        private List<Zone> zones;
        private static ZoneFactory instance;

        private ZoneFactory(XmlDocument zoneDataFile)
        {
            zones = new List<Zone>();

            XmlNode zs = zoneDataFile.GetElementsByTagName("Zones")[0];
            foreach (XmlNode zone in zs)
            {
                this.zones.Add(Zone.FromXmlNode(zone));
            }
        }

        /// <summary>
        /// Gets a loaded Zone by ZoneId
        /// </summary>
        /// <param name="zoneId">The zone unique identifier</param>
        /// <returns>A Zone</returns>
        public static Zone GetZoneById(String zoneId)
        {
            foreach (Zone z in instance.zones)
            {
                if (z.ZoneId == zoneId)
                    return z;
            }

            throw new ZoneNotFoundException(zoneId);
        }

        public static void InitializeFactory(XmlDocument zoneDataFile)
        {
            ZoneFactory zf = new ZoneFactory(zoneDataFile);
            instance = zf;
        }
    }
}
